import { ElMessage } from "element-plus";
import {
  computeTypeMap,
  doorColorMap,
  doorOpenAtMap,
  doorShapeMap,
  mainOrderCols,
  orderAttrsMap,
  selectKeys,
} from "../../utils/map";
import { createUniqueString } from "@/utils";
import orderAttrConfig from "@/utils/orderAttrConfig";
import { ref } from "vue";

const useOrderAttrStore = defineStore("order-attrs", {
  state: () => ({
    orderAttrs: [
      {
        name: "非标门系列",
        offerType: "",
        picture:
          "https://img1.jihui88.com/upload/1/16/13738960707/picture/2019/08/02/c5b3adf5-b469-43d1-9561-3736fb203c5a.jpg",
        type: "",
        category: "",
        brand: "",
        referPrice: 0,
        recommend: "一般",
        saleVolumes: 0,
        attrs: [
          {
            no: null,
            colWidth: null,
            attrName: null,
            attrChar: null,
            inputMode: null,
            selectOptions: null,
            inputType: null,
            isMixedDoor: null,
            visible: null,
            computeType: null,
            computeFormula: null,
            computeRemark: null,
          },
        ],
        grounding: true,
        default: true,
      },
      {
        name: "铸铝门系列",
        offerType: "",
        picture:
          "https://img1.jihui88.com/upload/1/16/13738960707/picture/2022/07/15/b7ba4103-c62b-4599-af30-4bab37ef9b17.jpg",
        type: "",
        category: "",
        brand: "",
        referPrice: 0,
        recommend: "一般",
        saleVolumes: 0,
        attrs: [
          {
            no: null,
            colWidth: null,
            attrName: null,
            attrChar: null,
            inputMode: null,
            selectOptions: null,
            inputType: null,
            isMixedDoor: null,
            visible: null,
            computeType: null,
            computeFormula: null,
            computeRemark: null,
          },
        ],
        grounding: true,
      },
      {
        name: "自定义",
        offerType: "",
        picture:
          "https://tse2-mm.cn.bing.net/th/id/OIP-C.f70rLSszxFJ_MGl9sM3V1QAAAA?rs=1&pid=ImgDetMain",
        type: "",
        category: "",
        brand: "",
        referPrice: 0,
        recommend: "一般",
        saleVolumes: 0,
        attrs: [
          {
            no: null,
            colWidth: null,
            attrName: null,
            attrChar: null,
            inputMode: null,
            selectOptions: null,
            inputType: null,
            isMixedDoor: null,
            visible: null,
            computeType: null,
            computeFormula: null,
            computeRemark: null,
          },
        ],
        grounding: true,
      },
    ],
    currentProductSeries: {},
    attrSearchValue: ''
  }),
  actions: {
    // 根据属性字典初始化属性管理数据
    initAttrs() {
      // const attrsDict = useDictStore().getDict("orderAttrs");
      this.orderAttrs.forEach((product) => {
        mainOrderCols.forEach((attr, index) => {
          let attrItem = {};
          attrItem.AttrId = createUniqueString();
          attrItem.AttrName = attr;
          attrItem.AttrKeyName = orderAttrsMap[attr];
          attrItem.AttrValue = [];
          attrItem.InputType = 1;
          attrItem.IsVisible = true;
          attrItem.ColumnWidth = 100;
          attrItem.FixType = 'none';
          attrItem.InputType2 = 0;
          if (selectKeys.includes(attr)) {
            attrItem.InputType = 0;
            switch (attr) {
              case "Field1":
                attrItem.AttrValue = Object.values(doorShapeMap);
                break;
              case "Field2":
                attrItem.AttrValue = Object.values(doorColorMap);
                break;
              case "Field3":
                attrItem.AttrValue = Object.values(doorOpenAtMap);
                break;
            }
            attrItem.ColumnWidth = 180;
          }
          product.attrs[index] = attrItem;
        });
      });
      this.mergeAttrs();
    },
    initAttrsAdd(name) {
      let product = this.orderAttrs.find((item) => item.name == name);
      mainOrderCols.forEach((attr, index) => {
        let attrItem = {};
        attrItem.no = index;
        attrItem.id = createUniqueString();
        attrItem.attrName = attr;
        attrItem.attrChar = orderAttrsMap[attr];
        attrItem.selectOptions = [];
        attrItem.inputType = "text";
        attrItem.isMixedDoor = false;
        attrItem.visible = true;
        attrItem.colWidth = 100;
        attrItem.fix = 'none';
        attrItem.compute = false;
        attrItem.computeType = "";
        attrItem.computeFormula = "";
        attrItem.computeRemark = "";
        attrItem.inputMode = "typing";
        if (selectKeys.includes(attr)) {
          attrItem.inputMode = "select";
          switch (attr) {
            case "Field1":
              attrItem.selectOptions = Object.values(doorShapeMap);
              break;
            case "Field2":
              attrItem.selectOptions = Object.values(doorColorMap);
              break;
            case "Field3":
              attrItem.selectOptions = Object.values(doorOpenAtMap);
              break;
          }
          attrItem.colWidth = 180;
        }
        if (["总金额"].includes(attrItem.attrChar)) {
          attrItem.compute = true;
          attrItem.computeType = "formula";
          attrItem.computeFormula = "门总高*门总宽";
          attrItem.computeRemark = "";
        }
        product.attrs[index] = attrItem;
      });
      this.mergeAttrs();
    },
    mergeAttrs() {
      orderAttrConfig.forEach((item) => {
        let product = this.orderAttrs.find((p) => p.name == item.productName);
        if (product) {
          let attr = product.attrs.find((a) => a.attrName == item.attrName);
          if (attr) {
            for (let key in item) {
              attr[key] = item[key];
            }
          }
        }
        if (item.productName == '*') {
          this.orderAttrs.forEach(p => {
            let attr = p.attrs.find((a) => a.attrName == item.attrName);
            if (attr) {
              for (let key in item) {
                attr[key] = item[key];
              }
            }
          });
        }
      });
    },
    getDefaultFormula(key, data) {
      let formula = this.orderAttrs
        .find((e) => e.default)
        .attrs.find((e) => e.attrName == key).computeFormula;
      for (let key in data) {
        if (orderAttrsMap[key])
          formula = formula
            .replaceAll(orderAttrsMap[key], data[key] || 0)
            // 公式里不要包含单位
            .replace(/[a-zA-Z]/g, "");
      }
      return formula;
    },
    updateData(attrsName, itemId, data) {
      let attrsSerial = this.orderAttrs.find(
        (attrs) => attrs.name == attrsName
      );
      if (attrsSerial) {
        let i = attrsSerial.attrs.findIndex((item) => item.id == itemId);
        if (i !== -1) {
          attrsSerial.attrs[i] = { ...attrsSerial.attrs[i], ...data };
          ElMessage.success("更新成功");
        } else {
          ElMessage.error("订单属性管理更新失败：找不到item");
        }
      } else {
        ElMessage.error("订单属性管理更新失败：name错误");
      }
    },
    setCurrentSeries(series) {
      this.currentProductSeries = series;
    }
  },
});

export default useOrderAttrStore;
